from django.shortcuts import render
from django.views.generic.base import View
from django.contrib.auth.mixins import LoginRequiredMixin
from apps.courses.models import Course,CourseTag, CourseResource,Video
from apps.users.models import UserProfile
from django.db.models import Q
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from apps.operation.models import UserFavorite,UserCourse,CourseComments,VideoComments
from apps.organizations.models import Teacher

class TeacherDetailView(View):
    def get(self, request, teacher_id, *args, **kwargs):
        teacher_all = Teacher.objects.all()
        teachers = Teacher.objects.get(id=teacher_id)
        courses = teachers.course_set.all()
        teacher_fav = teacher_all.order_by('-fav_nums')[:3]
        org = teachers.org
        teacher = Teacher.objects.get(id=teacher_id)
        teacher.click_nums += 1
        teacher.save()
        has_fav_teacher = False
        has_fav_org = False
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user, fav_id=teachers.id, fav_type=3):
                has_fav_teacher = True
            if UserFavorite.objects.filter(user=request.user, fav_id=org.id, fav_type=2):
                has_fav_org = True
        return render(request, 'teacher-detail.html', {
            'teachers': teachers,
            'courses': courses,
            'org': org,
            'teacher_fav': teacher_fav,
            'has_fav_teacher': has_fav_teacher,
            'has_fav_org': has_fav_org

        })

class TeacherView(View):

    def get(self, request, *args, **kwargs):
        teacher_all = Teacher.objects.all()
        keywords = request.GET.get('keywords', '')
        search_type = 'teacher'
        if keywords:
            teacher_all = teacher_all.filter(
                Q(name__icontains=keywords) | Q(points__icontains=keywords) | Q(work_company__icontains=keywords))
        sort = request.GET.get('sort', '')
        teacher_fav = teacher_all.order_by('-fav_nums')[:3]
        if sort == 'hot':
            teacher_all = teacher_all.order_by('-click_nums')
        teacher_nums = teacher_all.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        # 对课程机构数据进行分页
        p = Paginator(teacher_all, per_page=5, request=request)
        teachers = p.page(page)
        return render(request, 'teachers-list.html', {
            'teacher_nums': teacher_nums,
            'teacher_all': teacher_all,
            'teachers': teachers,
            'sort': sort,
            'teacher_fav': teacher_fav,
            'keywords': keywords,
            'search_type': search_type
        })

class CoursePlayerView(LoginRequiredMixin,View):
    login_url = '/login/'

    def get(self, request, video_id, *args, **kwargs):
        video = Video.objects.get(id=video_id)
        course_id = video.lesson.course.id
        course = Course.objects.get(id=course_id)
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        if not user_course:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students += 1
            course.save()
        course_resource = CourseResource.objects.filter(course=course)
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).exclude(course_id=course.id).order_by(
            '-course__click_nums')[:5]
        related_courses = [user_course.course for user_course in all_courses]
        comments = VideoComments.objects.filter(video_id=video.id).order_by('-add_time')

        return render(request, 'player.html', {
            'course': course,
            'course_resource': course_resource,
            'related_courses': related_courses,
            'comments': comments,
            'video': video
        })

class CourseCommentView(LoginRequiredMixin,View):
    login_url = '/login/'

    def get(self, request, course_id, *args, **kwargs):
        course = Course.objects.get(id=course_id)
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        if not user_course:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students +=1
            course.save()
        course_resource = CourseResource.objects.filter(course=course)
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).exclude(course_id=course.id).order_by('-course__click_nums')[:5]
        related_courses = [user_course.course for user_course in all_courses]
        comments = CourseComments.objects.filter(course_id=course.id).order_by('-add_time')

        return render(request, 'course-comment.html', {
            'course': course,
            'course_resource': course_resource,
            'related_courses': related_courses,
            'comments': comments
        })

class CourseVideoView(LoginRequiredMixin,View):
    login_url = '/login/'

    def get(self, request, course_id, *args, **kwargs):
        course = Course.objects.get(id=course_id)
        course.click_nums += 1
        course.save()
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        if not user_course:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students +=1
            course.save()
        course_resource = CourseResource.objects.filter(course=course)
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).exclude(course_id=course.id).order_by('-course__click_nums')[:5]
        related_courses = [user_course.course for user_course in all_courses]


        return render(request, 'course-video.html', {
            'course': course,
            'course_resource': course_resource,
            'related_courses': related_courses
        })

class CourseView(View):
    def get(self, request, *args, **kwargs):
        course_all = Course.objects.order_by('-add_time')
        hot_course = course_all.order_by('-click_nums')[:3]
        keywords = request.GET.get('keywords', '')
        search_type = 'course'
        if keywords:
            course_all = course_all.filter(Q(name__icontains=keywords) | Q(desc__icontains=keywords) | Q(tag__icontains=keywords))
        sort = request.GET.get('sort', '')
        if sort == 'hot':
            course_all = course_all.order_by('-click_nums')
        if sort == 'students':
            course_all = course_all.order_by('-students')
        if sort == 'times':
            course_all = course_all.order_by('-learn_times')
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        # 对课程机构数据进行分页
        p = Paginator(course_all, per_page=5, request=request)
        courses = p.page(page)
        return render(request, 'course-list.html', {
            'all_courses': courses,
            'sort': sort,
            'hot_course': hot_course,
            'keywords': keywords,
            'search_type': search_type
        })


class CourseDetailView(View):
    def get(self, request, course_id, *args, **kwargs):
        # 点击数
        course = Course.objects.get(id=course_id)
        course.click_nums += 1
        course.save()
        # hot_course = Course.objects.filter(tag=course.tag)
        # hot_course = hot_course.order_by('-click_nums').exclude(id=course.id)[:3]
        # 标签
        tags = course.coursetag_set.all()
        tag_list = [tag.tag for tag in tags]
        course_tags = CourseTag.objects.filter(tag__in=tag_list).exclude(course__id=course.id)
        related_courses = set()
        for course_tag in course_tags:
            related_courses.add(course_tag.course)
        has_fav_course=False
        has_fav_org = False
        if request.user.is_authenticated:
            if  UserFavorite.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
                has_fav_course = True
            if UserFavorite.objects.filter(user=request.user, fav_id=course.coures_org.id, fav_type=2):
                has_fav_org = True
        return render(request, 'course-detail.html',{
            'course': course,
            'related_courses': related_courses,
            'has_fav_course': has_fav_course,
            'has_fav_org': has_fav_org,
        })



